# coding:utf8
import pymysql
# # 存储读取每一行的数据
# map = {}
# 存储字段的列表
list = []
# 存储所有的读取的数据
list2 = []
# 连接数据库
connect = pymysql.Connect(host="localhost", port=3306, database="test_project", user="root", password="",
                          charset="utf8")
# sql语句
sql = "SELECT * from userinfo;"
# 获取游标
cursor = connect.cursor()
# 执行sql语句
cursor.execute(sql)
# 获取表头字段
dec = cursor.description
# 获取所有执行结果
result = cursor.fetchall()

# 遍历表头，存储到list中
for i in range(0, len(dec)):
    list.append(dec[i][0])
# 遍历所有行的数据
for j in range(0, len(result)):
    # 重置字典
    map={}
    # 遍历各行的每个数据
    for x in range(0, len(result[j])):
        # 键和值对应，存储为字典
        map[list[x]] = result[j][x]
        # print(map)
    # print(str(j)+str(map))
    list2.append(map)
    # print(str(j)+str(list2))
cursor.close()
connect.close()
print(list2)



# map = {}
# list = []
# list2=[]
# connect = pymysql.Connect(host="localhost", port=3306, database="test_project", user="root", password="123456",
#                           charset="utf8")
# sql = "SELECT * from userinfo;"
# cursor = connect.cursor()
# cursor.execute(sql)
# dec = cursor.description
# result = cursor.fetchall()
#
# # print(dec)
# for i in range(0, len(dec)):
#     # print(dec[i][0])
#     list.append(dec[i][0])
# print(list)
#
# # # print(result)
# # print(len(result))
# # for j in range(0, len(result)):
# #     # print(result[j])
# #     # print(result[j][0])
# #     # print(len(result[j]))
# #     for x in range(0, len(result[j])):
# #         print(result[j][x])
# #     print()
#
# # map["key2"]="value2"
# # print(map)
#
# for j in range(0, len(result)):
#     for x in range(0, len(result[j])):
#         map[list[x]] = result[j][x]
#     # print(j)
#     print(map)
#     list2.append(str(map))
#     # list2.append(map)
#     # print(list2)
# print(list2)
# print(list2[0])
# print(type(list2[0]))
# print(list2[0].get("id"))


# def mysql(sql):
#
#
#     list_dec = []
#     list_result = []
#     connect = pymysql.Connect(host="localhost", port=3306, database="test_project", user="root", password="123456",charset="utf8")
#     cursor = connect.cursor()
#     cursor.execute(sql)
#     dec = cursor.description
#     result = cursor.fetchall()
#     for i in range(0, len(dec)):
#         list_dec.append(dec[i][0])
#     for j in range(0, len(result)):
#         map = {}
#         for x in range(0, len(result[j])):
#             map[list_dec[x]] = result[j][x]
#         list_result.append(map)
#     return list_result
#
# print(mysql("SELECT * from userinfo;"))


